package dcc.component;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import dcc.mbg.model.User;
import dcc.tools.BeanFactory;
import dcc.tools.Tools;

public class JChangePasswordPanel extends JPanel {

	private User user;
	private JPasswordField pfNewPassword;
	private JPasswordField pfOkPassword;
	private JPasswordField pfOldPassword;

	public JChangePasswordPanel() {
		init();
		this.user = Tools.get().getUser();
	}

	private void init() {
		setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 415, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 1;
		add(panel, gbc_panel);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 100, 80, 80, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblOldPassword = new JLabel("\u8F93\u5165\u65E7\u5BC6\u7801:");
		lblOldPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		lblOldPassword.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15));
		GridBagConstraints gbc_lblOldPassword = new GridBagConstraints();
		gbc_lblOldPassword.anchor = GridBagConstraints.EAST;
		gbc_lblOldPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblOldPassword.gridx = 2;
		gbc_lblOldPassword.gridy = 2;
		panel.add(lblOldPassword, gbc_lblOldPassword);

		pfOldPassword = new JPasswordField();
		GridBagConstraints gbc_pfOldPassword = new GridBagConstraints();
		gbc_pfOldPassword.gridwidth = 2;
		gbc_pfOldPassword.insets = new Insets(0, 0, 5, 0);
		gbc_pfOldPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pfOldPassword.gridx = 3;
		gbc_pfOldPassword.gridy = 2;
		panel.add(pfOldPassword, gbc_pfOldPassword);

		JLabel lblNewPassword = new JLabel("\u8F93\u5165\u65B0\u5BC6\u7801:");
		lblNewPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNewPassword.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15));
		GridBagConstraints gbc_lblNewPassword = new GridBagConstraints();
		gbc_lblNewPassword.anchor = GridBagConstraints.EAST;
		gbc_lblNewPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewPassword.gridx = 2;
		gbc_lblNewPassword.gridy = 3;
		panel.add(lblNewPassword, gbc_lblNewPassword);

		pfNewPassword = new JPasswordField();
		GridBagConstraints gbc_pfNewPassword = new GridBagConstraints();
		gbc_pfNewPassword.gridwidth = 2;
		gbc_pfNewPassword.insets = new Insets(0, 0, 5, 0);
		gbc_pfNewPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pfNewPassword.gridx = 3;
		gbc_pfNewPassword.gridy = 3;
		panel.add(pfNewPassword, gbc_pfNewPassword);

		JLabel lblOkPassword = new JLabel("\u786E\u8BA4\u5BC6\u7801:");
		lblOkPassword.setHorizontalAlignment(SwingConstants.RIGHT);
		lblOkPassword.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15));
		GridBagConstraints gbc_lblOkPassword = new GridBagConstraints();
		gbc_lblOkPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblOkPassword.anchor = GridBagConstraints.EAST;
		gbc_lblOkPassword.gridx = 2;
		gbc_lblOkPassword.gridy = 4;
		panel.add(lblOkPassword, gbc_lblOkPassword);

		pfOkPassword = new JPasswordField();
		GridBagConstraints gbc_pfOkPassword = new GridBagConstraints();
		gbc_pfOkPassword.gridwidth = 2;
		gbc_pfOkPassword.insets = new Insets(0, 0, 5, 0);
		gbc_pfOkPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pfOkPassword.gridx = 3;
		gbc_pfOkPassword.gridy = 4;
		panel.add(pfOkPassword, gbc_pfOkPassword);

		JButton btnClear = new JButton("\u6E05\u9664");
		btnClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				clear();
			}
		});
		GridBagConstraints gbc_btnClear = new GridBagConstraints();
		gbc_btnClear.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnClear.insets = new Insets(0, 0, 5, 5);
		gbc_btnClear.gridx = 3;
		gbc_btnClear.gridy = 5;
		panel.add(btnClear, gbc_btnClear);

		JButton btnSave = new JButton("\u4FDD\u5B58");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.insets = new Insets(0, 0, 5, 0);
		gbc_btnSave.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnSave.gridx = 4;
		gbc_btnSave.gridy = 5;
		panel.add(btnSave, gbc_btnSave);
	}

	private void clear() {
		this.pfOldPassword.setText("");
		this.pfNewPassword.setText("");
		this.pfOkPassword.setText("");
	}

	private void save() {
		if (String.valueOf(this.pfOldPassword.getPassword()).equals("")
				|| String.valueOf(this.pfNewPassword.getPassword()).equals("")
				|| String.valueOf(this.pfOkPassword.getPassword()).equals("")) {
			JOptionPane.showMessageDialog(
					this,
					Tools.get().getValue(
							"LABEL_INPUT_OLDPASSWORD_NEWPASSWORD_OKPASSWORD"),
					Tools.get().getValue("LABEL_PROMPT_TITLE"),
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (!this.user.getPassword().equals(
				String.valueOf(this.pfOldPassword.getPassword()))) {
			// TODO old passwrod is wrong
			JOptionPane.showMessageDialog(this,
					Tools.get().getValue("LABEL_INPUT_OLDPASSWORD_IS_WRONG"),
					Tools.get().getValue("LABEL_PROMPT_TITLE"),
					JOptionPane.ERROR_MESSAGE);
		} else {
			if (!String.valueOf(this.pfNewPassword.getPassword()).equals(
					String.valueOf(this.pfOkPassword.getPassword()))) {
				// TODO new password is different from ok password
				JOptionPane
						.showMessageDialog(
								this,
								Tools.get()
										.getValue(
												"LABEL_INPUT_NEWPASSWORD_DIFFERENT_OKPASSWORD"),
								Tools.get().getValue("LABEL_PROMPT_TITLE"),
								JOptionPane.ERROR_MESSAGE);

			} else {
				// TODO update password in DB
				this.user.setPassword(String.valueOf(this.pfNewPassword
						.getPassword()));
				try {
//					((UserService) BeanFactory.getBean("userService"))
//							.changePassword(this.user);
//					JOptionPane.showMessageDialog(this,
//							Tools.get().getValue("LABEL_SAVE_OK_PROMPT"), Tools
//									.get().getValue("LABEL_PROMPT_TITLE"),
//							JOptionPane.INFORMATION_MESSAGE);
				} catch (Exception e) {
					e.printStackTrace();
					JOptionPane.showMessageDialog(this,
							Tools.get().getValue("LABEL_SAVE_ERROR_PROMPT"),
							Tools.get().getValue("LABEL_PROMPT_TITLE"),
							JOptionPane.ERROR_MESSAGE);
				}
				clear();
			}
		}
	}
}
